home *** CD-ROM | disk | FTP | other *** search
/ Your Choice 3 / Your Choice Software Collection 3.iso / os2 / grfcdemo / demo.@ / CLRTEST.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-04  |  10.0 KB  |  253 lines

  1. /****************************************************************************
  2.     (c) 1984-1993 by Scientific Endeavors Corporation.
  3.     All rights reserved.
  4.  
  5.     This program demonstrates a semi-log plot with a legend, a linear plot
  6.     with a window, and a semi-log plot with left and right y axes and
  7.     upright y labels.
  8. ****************************************************************************/
  9. #include <graphic.h>                           /* Include all needed files */
  10.  
  11. #if TCQ                                 /* Set stack for Borland (Turbo) C */
  12. extern unsigned _stklen = 0x3000;
  13. #endif
  14.  
  15. /* Declare variables here so they don't go on the stack */
  16. static char *xstring[] = {                    /* Strings for X-Axis labels */
  17.     "1984", "1985", "1986", "1987", "1988", "1989"
  18. };
  19. static float u1[62] = {                    /* u1 is the Company stock fund */
  20.     6.74, 7.3, 8.61, 9.29, 9.74, 10., 9.676, 10.6038, 11.7344, 11.3485,
  21.     13.7248, 12.7133, 13.4976, 15.498, 16.3235, 15.8140, 15.7405, 17.8188,
  22.     17.8277, 18.0340, 17.734, 15.2812, 15.2875, 16.172, 16.671, 15.6087,
  23.     17.9595, 20.7653, 20.9448, 22.5165, 22.1078, 20.95, 22.9562, 20.9474,
  24.     19.1381, 21.3239, 18.8562, 21.7838, 22.8548, 24.4238, 21.9162, 21.9184,
  25.     22.967, 24.9581, 26.3963, 27.2202, 19.4238, 18.9284, 20.9032, 23.4342,
  26.     23.3657, 22.2842, 21.2772, 20.5066, 22.5556, 20.7719, 19.7649, 21.7906,
  27.     22.5653, 21.3561, 20.6527, 21.562
  28. };
  29. static float u2[62] = {                     /* u2 is the Fixed Income Fund */
  30.     24.9266, 25.1392, 25.3568, 25.5743, 25.8139, 26.0681, 26.3109, 26.5607,
  31.     26.8234, 27.0789, 27.3422, 27.6022, 27.8693, 28.1191, 28.3711, 28.6253,
  32.     28.8833, 29.1397, 29.3996, 29.6653, 29.9315, 30.2079, 30.4776, 30.7508,
  33.     31.047, 31.3164, 31.5869, 31.8565, 32.1266, 32.3961, 32.6705, 32.95,
  34.     33.2245, 33.5053, 33.7878, 34.0731, 34.4021, 34.6832, 34.9363, 35.2175,
  35.     35.4915, 35.7733, 36.0476, 36.3593, 36.627, 36.9042, 37.1871, 37.4632,
  36.     37.7509, 38.0319, 38.2865, 38.5704, 38.8489, 39.1397, 39.4259, 39.7232,
  37.     40.0245, 40.3181, 40.6301, 40.932, 41.2492, 41.5818
  38. };
  39. static float u3[62] = {                           /* u3 is the Equity Fund */
  40.     18.26, 18.2983, 18.346, 17.4876, 17.7345, 16.9745, 17.33, 17.087,
  41.     18.6332, 18.7496, 18.7721, 18.7327, 19.4395, 20.4982, 20.6580, 21.0312,
  42.     21.3133, 22.2778, 22.8659, 22.681, 22.684, 22.1318, 23.2726, 24.388,
  43.     25.2836, 25.6543, 27.4712, 29.1251, 28.4984, 29.6629, 30.1036, 28.98,
  44.     31.5469, 29.8584, 30.4436, 30.8878, 30.6971, 33.8124, 34.8253, 35.8124,
  45.     36.322, 36.57, 38.0806, 38.6662, 40.3914, 38.4364, 31.9274, 29.2592,
  46.     31.0548, 34.7917, 35.4516, 34.3014, 34.6729, 35.8435, 38.9324, 38.7803,
  47.     37.4419, 39.6724, 40.7028, 40.1896, 40.1600, 43.6935
  48. };
  49.  
  50. #ifdef ANSI
  51. void GPC_MAIN(void)
  52. #else
  53. void GPC_MAIN()
  54. #endif
  55. {
  56.     float t[62];
  57.     double x1a, pi = 3.141592654;
  58.     int nxdiv, nydiv, npts;
  59.     float shrs1[62], shrs2[62], shrs3[62], vl1[62], vl2[62], vl3[62];
  60.     float taly1, taly2, taly3;
  61.     int i, inum1, inum1m;
  62.     G_PFQ(x, hx);
  63.     G_PFQ(y, hy);
  64.     G_PFQ(z, hz);
  65.  
  66.     x = (float DIST *)GPC_ALLOC(&hx, 301, sizeof(float));
  67.     y = (float DIST *)GPC_ALLOC(&hy, 301, sizeof(float));
  68.     z = (float DIST *)GPC_ALLOC(&hz, 301, sizeof(float));
  69.     if (x == (void DIST *)NULL || y == (void DIST *)NULL ||
  70.         z == (void DIST *)NULL) {
  71.         GPC_PUTS("CLRTEST: Could not allocate data arrays");
  72.         goto FreeMem;
  73.     }
  74.     inum1 = 62;                                    /* Generate data points */
  75.     inum1m = inum1-1;
  76.     taly1 = 0.;
  77.     taly2 = 0.;
  78.     taly3 = 0.;
  79.     for(i = 0; i <= inum1m; i++) {
  80.         t[i] = i;
  81.         taly1 = taly1 + 1. / u1[i];
  82.         shrs1[i] = taly1;
  83.         vl1[i] = shrs1[i] * u1[i];
  84.  
  85.         taly2 = taly2 + 1. / u2[i];
  86.         shrs2[i] = taly2;
  87.         vl2[i] = shrs2[i] * u2[i];
  88.  
  89.         taly3 = taly3 + 1. / u3[i];
  90.         shrs3[i] = taly3;
  91.         vl3[i] = shrs3[i] * u3[i];
  92.     }
  93.  
  94.     bgnplot(1, 'g', "clr.tkf");                  /* GraphiC initialization */
  95.     startplot(BLACK);
  96.     metricunits(0);                                 /* Plots are in inches */
  97.     font(2, "complex.fnt", '\310', "microb.fnt", '\311');  /* Select fonts */
  98.     rotate(1);                                          /* Plot is rotated */
  99.  
  100.     page(6.884f, 9.0f);                      /* Size of page and plot area */
  101.     area2d(5.5f, 7.6f);
  102.  
  103.     color(WHITE);
  104.     tcurve(4);                            /* Curves will be 4 pixels thick */
  105.     legpos(3, 2.5, 2.5, 1);                                 /* Draw legend */
  106.     legend(1, "Company", 1, .14, 9);
  107.     legend(1, "Fixed Income", 4, .14, 12);
  108.     legend(1, "Equity Fund", 9, .14, 13);
  109.  
  110.     yname("Price ($ per unit)");                   /* Axis and plot titles */
  111.     heading("\311Savings Plan Values\310");
  112.     frame(1,1);                               /* Semi-log axis with frame, */
  113.     grid(2);                                           /* Dotted grid, and */
  114.     fgrid(0,0);
  115.     xlab(1,xstring);                                      /* String labels */
  116.     ylog("", 0., 12., 59., 6., 60.);
  117.     xlab(0,xstring);
  118.  
  119.     curve((float DIST *)t, (float DIST *)u1, inum1, 0);
  120.     curve((float DIST *)t, (float DIST *)u2, inum1, 0);
  121.     curve((float DIST *)t, (float DIST *)u3, inum1, 0);
  122.     tcurve(0);
  123.  
  124.     if (endplot())                                 /* Terminate first plot */
  125.         goto EndOfApp;
  126.  
  127.     startplot(MAGENTA);               /* GraphiC initialization (2nd plot) */
  128.     font(3, "simplex.fnt", '\310', "triplex.fnt", '\311',  /* Select fonts */
  129.         "complex.fnt", '\312');
  130.     rotate(0);                                      /* Non rotated picture */
  131.  
  132.     page(9.0f, 6.884f);                      /* Size of page and plot area */
  133.     area2d(7.6f, 5.5f);
  134.  
  135.     gwindow(4.0, 1.0, 8.8, 4.25);                /* Define area for window */
  136.  
  137.     color(YELLOW);                                 /* Axis and plot titles */
  138.     xname("\312X axis");
  139.     yname("\312Y axis");
  140.     heading("\311WINDOW SAMPLE");
  141.  
  142.     npts=301;                                      /* Generate data points */
  143.     for(i = 0; i < npts; i++) {
  144.         y[i] = .3 * i;
  145.         x[i] = (y[i] * y[i]) / 2;
  146.     }
  147.  
  148.     color(BLACK);
  149.     nxdiv=5;                              /* Desired # of x-axis divisions */
  150.     nydiv=6;                              /* Desired # of y-axis divisions */
  151.     frame(0,0);                                          /* Turn off frame */
  152.     fgrid(0,0);                               /* Turn off grid subdivision */
  153.     grid(9);                                /* Fine dot grid at tick marks */
  154.     box();
  155.     scales(nxdiv, nydiv, (float DIST *)x, (float DIST *)y, npts);
  156.  
  157.     color(BRT_WHITE);
  158.     dashf(6);
  159.     curve((float DIST *)x, (float DIST *)y, npts, 0); /* Draw curve only */
  160.     endplot();
  161.  
  162. /*
  163.     The following calls assume that the plot fills the entire screen.
  164.     However, all plot elements will be scaled to fit within the area
  165.     defined in the call to gwindow(). setscale(1) turns this mode on.
  166. */ 
  167.     startplot(MAGENTA);/* Start the picture that goes in the windowed area */
  168.     setscale(1);                                   /* Turn on font scaling */
  169.     font(2, "swissbld.fnt", '\310', "triplex.fnt", '\311');/* Select fonts */
  170.     fillfont(1);
  171.  
  172.     color(BLACK);                            /* Draw black box around plot */
  173.     box();
  174.  
  175.     color(BLUE);                           /* Plot blue text in the window */
  176.     tmargin(1.75);
  177.     lmargin(.25);
  178.     ltline("\310GraphiC \311allows you to leave an", .325);
  179.     ltline("\311area within a plot clear and", .325);
  180.     ltline("\311create a new plot in the clear", .325);
  181.     ltline("\311area.", .325);
  182.  
  183.     setscale(0);                                  /* Turn off font scaling */
  184.  
  185.     if (endplot())                                   /* Terminate 2nd plot */
  186.         goto EndOfApp;
  187.  
  188.     startplot(BLACK);                 /* GraphiC initialization (3rd plot) */
  189.     font(4, "simplex.fnt", '\310', "triplex.fnt", '\311',  /* Select fonts */
  190.         "complex.fnt", '\312', "compgrma.fnt", '\313');
  191.  
  192.     page(9.0f, 6.884f);                      /* Size of page and plot area */
  193.     area2d(6.4f, 5.5f);
  194.  
  195.     color(MAGENTA);                                 /* Draw magenta legend */
  196.     legpos(2, 5.3, 5.1, 1);
  197.     legend(1, "Amplitude", 1, .125, 14);
  198.     legend(1, "Phase", 4, .125, 12);
  199.  
  200.     pbox();                                  /* Draw a box around the plot */
  201.     xname("\312Frequency");                                /* x axis label */
  202.     yname("\312Amplitude");                                /* y axis label */
  203.     heading("\311Filter Response");                          /* Plot title */
  204.  
  205.     color(WHITE);
  206.     nxdiv=10;                             /* Desired # of x axis divisions */
  207.     nydiv=6;                              /* Desired # of y axis divisions */
  208.     grid(2);                              /* Grid lines on the x-axis only */
  209.     fgrid(-1, 5);                   /* Subdivide axes with tick marks only */
  210.     frame(1,0);                       /* Frame the plot with no tick marks */
  211.     rightax(1);                      /* Second y-axis will be on the right */
  212.     upright(1);                         /* y-axis labels are to be upright */
  213.     xlog(0.05, 10.0, "%-1.2f", 0.0, 0.4, 2.4);
  214.  
  215.     npts = 200;                                    /* Generate data points */
  216.     for(i = 1; i <= 200; i++) {
  217.         x[i-1] = (float)i / 20.;
  218.         x1a = x[i-1] * x[i-1];
  219.         y[i-1] = 1 / sqrt((double)((1 - x1a) * (1 - x1a)) +
  220.             ((.5 * x[i-1]) * (.5 * x[i-1])));
  221.         if(i == 20)
  222.             z[i-1] = pi / 2;
  223.         else
  224.             z[i-1] = -atan((double)(.5 * x[i-1] / (1. - x1a)));
  225.         if(z[i-1] > 0)
  226.             z[i-1] -= pi;
  227.     }
  228.  
  229.     curve((float DIST *)x, (float DIST *)y, npts, 0); /* Draw curve only */
  230.  
  231.     color(MAGENTA);
  232.     yname("\312Phase in Radians");              /* Right-hand y axis label */
  233.  
  234.     color(WHITE);
  235.     frame(0, 0);                                    /* Do not redraw frame */
  236.     fgrid(0, 0);                                  /* Do not subdivide plot */
  237.     xlog(0.05, 10.0, "%-1.1f", -3.6, 0.6, 0.0);  /* Draw right-hand y-axis */
  238.  
  239.     curve((float DIST *)x, (float DIST *)z, npts, 0); /* Draw curve only */
  240.  
  241.     dateit('\312');                           /* Put date and time on plot */
  242.  
  243.     endplot();                                       /* Terminate 3rd plot */
  244.  
  245. EndOfApp:
  246.     stopplot();                                    /* Quit and close files */
  247.  
  248. FreeMem:
  249.     GPC_FREE(&hx, &x);        /* free allocated global memory */
  250.     GPC_FREE(&hy, &y);
  251.     GPC_FREE(&hz, &z);
  252. }
  253.